package com.gitee.feizns;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Verticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;

/**
 * Hello world!
 */
public class App {

    /**
     * Vertex
     */
    public static final Vertx vertx = Vertx.vertx();

    /**
     * Router
     */
    public static final Router router = Router.router(vertx);

    public static final Verticle verticle = new AbstractVerticle() {

        /**
         *
         */
        private int count;

        @Override
        public void start() throws Exception {
            super.start();
            System.out.println("HELLO WORLD");
            Route route = router.route("/test");
//            vertx.websocket()
            vertx.eventBus().consumer("test", msg -> {
                System.out.println("接收道消息 : " + msg.body());
                if ( ((String) (msg.body())).lastIndexOf("25") != -1 ) {
                    System.out.println("停止部署");
//                    vert
                    vertx.undeploy(id);
                }
            });
            route.handler(ctx -> {
                System.out.println("First HANDLER");
                HttpServerResponse response = ctx.response();
                response.setChunked(true);
                response.write("First HANDLER");
                ctx.next();
            });
            route.handler(ctx -> {
                System.out.println("Second HANDLER");
                ctx.response().write("Second HANDLER");
                ctx.next();
            });
            route.handler(ctx -> {
                System.out.println("Third HANDLER");
                ctx.response().end("Third HANDLER");
            });
        }

    };

    public static String id = "";

    /**
     * 计数
     */
    public static int count;

    public static void main(String[] args) {
        vertx.createHttpServer().requestHandler(router).listen(8080).onSuccess(server -> {
            System.out.println("Vertx server create on port " + server.actualPort());
        });

        vertx.eventBus().consumer("test", msg -> {
            System.out.println("HELLO WORLD!!!" + msg.body());
        });

        vertx.setPeriodic(1, id -> vertx.eventBus().publish("test", "MSG: COUNT IS : " + count++));

        vertx.deployVerticle(verticle).onSuccess(id -> App.id = id);

//        Vertx vertx = Vertx.vertx();
//        Promise<String> promise = Promise.<String>promise();
//        Future<String> future = promise.future().onSuccess(System.out::println);
////        future.
////        promise.future().onSuccess(System.out::println);
//        HttpServer server = vertx.createHttpServer();
//        server.requestHandler(req -> {
//            System.out.println(req.absoluteURI());
//            if ( "1".equals(req.getParam("name")) )
//                promise.complete("HELLO WORLD          " + req.absoluteURI());
//            req.response().end("HELLO WorLD  " + req.absoluteURI());
//        });
//        server.listen(8080).onSuccess(req -> {
//            System.out.printf("在端口:%s 启动成功.%n", req.actualPort());
//        });
//        vertx.setPeriodic(1000, id -> {
//            // 这个处理器将会每隔一秒被调用一次
//            System.out.println("timer fired!");
//        });
//        .complete("HELLO WORLD");
    }



}
